package com.hlm.demo.service.account;

import com.hlm.demo.entity.Account;
import com.hlm.demo.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccountService implements IAccountService {

    @Autowired
    private AccountMapper accountMapper;

    /**
     * 查询出所有的账单表数据的集合
     *
     * @param page  页数
     * @param count 每页显示的数据条数
     * @return
     */
    @Override
    public List<Account> findAllAccount(Integer page, Integer count, String idSearch, String nameSearch, String usernameSearch, String stateSearch) {
        Map<String, Object> map = new HashMap<>();
        Integer offset = (page - 1) * count;
        map.put("offset", offset);
        map.put("count", count);
        if (!"不验证".equals(idSearch) && !"".trim().equals(idSearch)) {
            map.put("idcardNo", idSearch);
        }
        if (!"不验证".equals(nameSearch) && !"".trim().equals(nameSearch)) {
            map.put("realName", nameSearch);
        }
        if (!"不验证".equals(usernameSearch) && !"".trim().equals(usernameSearch)) {
            map.put("loginName", usernameSearch);
        }
        System.out.println("-----------------" + stateSearch);
        switch (stateSearch) {
            case "全部":
                break;
            case "开通":
                map.put("status", 1);
                break;
            case "暂停":
                map.put("status", 2);
                break;
            case "删除":
                map.put("status", 3);
                break;
        }

        return accountMapper.findAllAccount(map);
    }

    /**
     * 账单表总数
     *
     * @return
     */
    @Override
    public Integer totalCount(String idSearch, String nameSearch, String usernameSearch, String stateSearch) {
        Map<String,Object> map = new HashMap<>();

        if (!"不验证".equals(idSearch) && !"".trim().equals(idSearch)) {
            map.put("idcardNo", idSearch);
        }
        if (!"不验证".equals(nameSearch) && !"".trim().equals(nameSearch)) {
            map.put("realName", nameSearch);
        }
        if (!"不验证".equals(usernameSearch) && !"".trim().equals(usernameSearch)) {
            map.put("loginName", usernameSearch);
        }
        System.out.println("-----------------" + stateSearch);
        switch (stateSearch) {
            case "全部":
                break;
            case "开通":
                map.put("status", 1);
                break;
            case "暂停":
                map.put("status", 2);
                break;
            case "删除":
                map.put("status", 3);
                break;
        }

        return accountMapper.totalCount(map);
    }

    /**
     * 根据id查找某一条账单数据
     *
     * @param accountId
     * @return
     */
    @Override
    public Account findAccountById(Integer accountId) {
        return accountMapper.findAccountById(accountId);
    }

    /**
     * 根据id更新某一条账单数据
     *
     * @param account
     * @return
     */
    @Override
    public Integer updAccount(Account account) {
        Map<String, Object> map = new HashMap<>();
        Integer accountId = account.getAccountId();
        map.put("accountId", accountId);

        String realName = account.getRealName();
        String occupation = account.getOccupation();
        String telephone = account.getTelephone();
        String email = account.getEmail();
        String mailAddress = account.getMailaddress();
        String zipcode = account.getZipcode();
        String qq = account.getQq();
        Account accountById = accountMapper.findAccountById(accountId);

        if (!accountById.getRealName().equals(realName)) {
            map.put("realName", realName);
        }
        if (!accountById.getOccupation().equals(occupation)) {
            map.put("occupation", occupation);
        }
        if (!accountById.getTelephone().equals(telephone)) {
            map.put("telephone", telephone);
        }
        if (!accountById.getEmail().equals(email)) {
            map.put("email", email);
        }
        if (!accountById.getMailaddress().equals(mailAddress)) {
            map.put("mailaddress", mailAddress);
        }
        if (!accountById.getZipcode().equals(zipcode)) {
            map.put("zipcode", zipcode);
        }
        if (!accountById.getQq().equals(qq)) {
            map.put("qq", qq);
        }
        if (map.size() == 1) {
            return 0;
        } else {
            return accountMapper.updAccount(map);
        }


    }

    /**
     * 根据id更新状态码
     *
     * @param accountId
     * @param status
     * @return
     */

    @Override
    public Integer updStatus(Integer accountId, String status) {
        Map<String, Object> map = new HashMap<>();
        // TODO: 数据的非法性校验
        map.put("accountId", accountId);
        map.put("status", status);
        if ("1".equals(status)) {
            map.put("createDate", 1);
        }
        if ("2".equals(status)) {
            map.put("pauseDate", 2);
            accountMapper.updServicePauStatus(accountId);
        }
        if ("3".equals(status)) {
            map.put("closeDate", 3);
            accountMapper.updServiceDelStatus(accountId);
        }
        return accountMapper.updStatus(map);
    }

    /**
     * 添加一条账务信息
     *
     * @param account
     * @return
     */
    @Override
    public Integer addAccount(Account account) {
        Map<String, Object> map = new HashMap<>();
        map.put("recommenderId", account.getRecommenderId());
        map.put("loginName", account.getLoginName());
        map.put("loginPasswd", account.getLoginPasswd());
        map.put("realName", account.getRealName());
        map.put("idcardNo", account.getIdcardNo());
        map.put("birthdate", account.getBirthdate());
        map.put("gender", account.getGender());
        map.put("occupation", account.getOccupation());
        map.put("telephone", account.getTelephone());
        map.put("email", account.getEmail());
        map.put("mailAddress", account.getMailaddress());
        map.put("zipCode", account.getZipcode());
        map.put("qq", account.getQq());

        System.out.println("Service层得到的数据啊：：：" + account);

        return accountMapper.addAccount(map);
    }
}
